<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("cms/core/helper/Abstract.php");

class PHP_CMS_View_Helper_Route extends PHP_CMS_View_Helper_Abstract {

    public function route($routes=array(), $form=null, $rows=null) {

        $route = "";

        if(is_array($routes)) {
            for($i=0; $i<count($routes); $i++) {
                if(is_array($routes[$i])) {
                    $route .= array_key_exists("href", $routes[$i]) ?
                        "<a href=\"".$routes[$i]["href"]."\">".$routes[$i]["name"]."</a>" :
                        $routes[$i]["name"];

                    if($i<count($routes)-1)
                        $route .= " / ";
                } else {
                    $route .= $routes[$i];
                }
            }
        } else {
            $route .= $routes;
        }
        ?>

        <div class="route">
            <div class="left"><?=$route?></div>
            <? if($form !== null && $rows !== null) { ?>
            <div class="right"><?=$this->page($form, $rows)?> | <?=$this->info($form, $rows)?></div>
            <? } ?>
        </div>

        <?
    }

    public function info($form, $rows) {
        $start = $form->getPage() * $form->getResults();
        $end   = $rows < $start + $form->getResults() || $form->getResults() == "" ? $rows : $start + $form->getResults();
?>
<?=$this->message("label.showing", _Core::BUNDLE)?>: <?=$rows > 0 ? $start + 1 : 0?> <?=$this->message("label.showing.to", _Core::BUNDLE)?> <?=$end?> | <?=$this->message("label.showing.of", _Core::BUNDLE)?>: <?=$rows?><?
    }

    public function page($form, $rows) {
        $results = ($form->getResults() != "" && $rows > 0) ? ceil($rows / $form->getResults()) : 1;
        $start = $form->getPage() * $form->getResults();
        $end   = $start + $rows < $start + $form->getResults() ? $start + $rows : $start + $form->getResults();
?>
<?=$this->message("label.results", _Core::BUNDLE)?>: <? for($i=0; $i<$results; $i++) { ?><?=($i != $form->getPage()) ? "<a href=\"javascript:setPage('".get_class($form)."',".$i.")\">".($i + 1)."</a>" : ($i + 1)?><?=($i < $results - 1) ? "&nbsp;|&nbsp;" : ""?><? } ?><?
    }

}

?>